package com.fsquare.inventory.domain;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.broadleafcommerce.common.presentation.AdminPresentation;
import org.broadleafcommerce.openadmin.web.form.entity.Field;

public class AdminFieldsHandler implements IAdminFieldsHandler{
	
	public Set<Field> getHeaderFields(){
		Set<Field> headerFields = new HashSet<Field>();
		java.lang.reflect.Field[] fields = this.getClass().getDeclaredFields();
		for (java.lang.reflect.Field field : fields) {
			try {
				
				if(field.isAnnotationPresent(AdminPresentation.class)) {
					AdminPresentation adminPresentation = field.getAnnotation(AdminPresentation.class);
					Field blField = new Field()
					.withName(field.getName())
					.withColumnWidth(adminPresentation.columnWidth())
					.withFieldType(adminPresentation.fieldType().toString())
					.withFriendlyName(adminPresentation.friendlyName())
					.withHelp(adminPresentation.helpText()).withFilterSortDisabled(true)
					.withHint(adminPresentation.hint());					
					headerFields.add(blField);
				}
					
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} 

        }
		
		return headerFields;
		
	}

	@Override
	public Map<String, Object> getEntityMap() {
		java.lang.reflect.Field[] fields = this.getClass().getDeclaredFields();
		Map<String, Object> map = new HashMap<String, Object>();
	
		for (java.lang.reflect.Field field : fields) {
			try {
				map.put(field.getName(), field.get(this));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}

        }
		return map;
	}
}
